package com.sljy.hzk.view.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.sljy.hzk.R;
import com.sljy.hzk.util.DensityUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class ProgressAdapter  extends RecyclerView.Adapter<ProgressAdapter.MyViewHolder>  {

    private Context context;
    private List<String> weekList;
    private int mClickPositon;
    private List<Integer> highColorList ;

    public ProgressAdapter(Context context, List<String> weekList) {
        this.context = context;
        this.weekList = weekList;
    }

    private OnProgressClickListener onProgressClickListener;
    public interface OnProgressClickListener{
        void scrollMid(int position);
    }
    public void setOnProgressClickListener(OnProgressClickListener onProgressClickListener){
        this.onProgressClickListener = onProgressClickListener;
    }

    public void changePostion(int mClickPositon){
        if(this.mClickPositon != mClickPositon){
            this.mClickPositon = mClickPositon;
            notifyDataSetChanged();
        }
    }

    public void setHighBackGround(List<Integer> positionList){
        if(highColorList == null)
            highColorList = new ArrayList<Integer>();
        highColorList.clear();
        highColorList.addAll(positionList);
        notifyDataSetChanged();
    }

    public boolean isHigh(int positon){
        if(highColorList == null)return false;
        if(highColorList.size() < positon+1)
            return false;
        return highColorList.get(positon) == 1 ? true : false;
    }

    @Override
    public ProgressAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View weekView = View.inflate(context, R.layout.progress_item, null);
        MyViewHolder viewHolder = new MyViewHolder(weekView);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(final ProgressAdapter.MyViewHolder holder, final int position) {
        String s = weekList.get(position);
        holder.tvText.setText(s);
        if(isHigh(position))
            holder.tvText.setBackgroundResource(R.drawable.shape_progress_item2);
        else{
            holder.tvText.setBackgroundResource(R.drawable.shape_progress_item);
        }
        holder.itemView.setTag(position);
        if (mClickPositon ==position){
            //将点击到的控件变大
            holder.tvText.setTextSize(20);
            FrameLayout.LayoutParams layoutParams = (FrameLayout.LayoutParams) holder.tvText.getLayoutParams();
            layoutParams.width =  DensityUtil.dip2px(context,40);
            layoutParams.height = DensityUtil.dip2px(context,40);
            layoutParams.topMargin = DensityUtil.dip2px(context,0);
            holder.tvText.setLayoutParams(layoutParams);
        }else {
            //将未点击的控件还原
            holder.tvText.setTextSize(16);
            FrameLayout.LayoutParams layoutParams = (FrameLayout.LayoutParams) holder.tvText.getLayoutParams();
            layoutParams.width = DensityUtil.dip2px(context,30);
            layoutParams.height = DensityUtil.dip2px(context,30);
            layoutParams.topMargin = DensityUtil.dip2px(context,15);
            holder.tvText.setLayoutParams(layoutParams);
        }
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onProgressClickListener!=null){
                    onProgressClickListener.scrollMid(position);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return weekList.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder{
        @BindView(R.id.fl_view)
        FrameLayout flView;
        @BindView(R.id.tv_text)
        TextView tvText;

        MyViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }

}
